package com.cqs.leetcode.tree;

import com.cqs.leetcode.TreeNode;

/**
 * @author lixqw
 * @date 2020/8/15
 */
public class LCOF {

    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        int left = hight(root.left);
        int right = hight(root.right);
        return Math.abs(left - right) < 2 && isBalanced(root.left) && isBalanced(root.right);
    }


    public int hight(TreeNode root) {
        if (root == null) return 0;
        int left = 0;
        if (root.left != null) {
            left = hight(root.left);
        }
        int right = 0;
        if (root.right != null) {
            right = hight(root.right);
        }
        return 1 + Math.max(left, right);
    }
}
